<template>
  <div style="margin: 20px 0">
    <el-table style="width: 100%" border :data="records">
      <el-table-column type="index" label="序号" width="80px" align="center">
      </el-table-column>
      <el-table-column prop="skuName" label="名称" width="width">
      </el-table-column>
      <el-table-column prop="skuDesc" label="描述" width="width">
      </el-table-column>
      <el-table-column label="默认图片" width="110px">
        <template slot-scope="{ row, $index }">
          <img
            :src="row.skuDefaultImg"
            alt="图片加载失败"
            style="width: 80px; height: 80px"
          />
        </template>
      </el-table-column>
      <el-table-column prop="weight" label="重量" width="80px">
      </el-table-column>
      <el-table-column prop="price" label="价格" width="120px">
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="width">
        <template slot-scope="{ row, $index }">
          <a title="下架" v-if="row.isSale == 1"
            ><el-button
              type="success"
              icon="el-icon-bottom"
              size="mini"
              class="buttons"
              @click="cancel(row)"
            ></el-button
          ></a>
          <a title="上架" v-else
            ><el-button
              type="warning"
              icon="el-icon-top"
              size="mini"
              class="buttons"
              @click="sale(row)"
            ></el-button
          ></a>
          <a title="修改"
            ><el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              class="buttons"
              @click="edit"
            ></el-button
          ></a>
          <a title="查看详情"
            ><el-button
              type="info"
              icon="el-icon-info"
              size="mini"
              class="buttons"
              @click="getSkuIfo(row)"
            ></el-button
          ></a>
          <a title="删除"
            ><el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              class="buttons"
            ></el-button
          ></a>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="text-align: center"
      :current-page="page"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      layout=" prev, pager, next, jumper,->,sizes,total"
      :total="total"
      @current-change="getSkuList"
      @size-change="handleSizeChange"
    >
    </el-pagination>
    <!--抽屉-->
    <el-drawer :visible.sync="show" :show-close="false" size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}元</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <el-tag
            type="success"
            v-for="attr in skuInfo.skuAttrValueList"
            :key="attr.id"
            style="margin: 0 5px 5px 0"
            size="mini"
          >
            {{ attr.attrId }}-{{ attr.valueId }}</el-tag
          >
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel height="450px" trigger="click">
            <el-carousel-item
              v-for="item in skuInfo.skuImageList"
              :key="item.id"
            >
              <img :src="item.imgUrl" alt="图片加载失败" class="carousel-img" />
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "Sku",
  data() {
    return {
      page: 1,
      limit: 10,
      records: [], //存储sku列表的数据
      total: 0,
      skuInfo: {}, //存储sku详情信息
      show: false, //控制抽屉显示与隐藏
    };
  },
  mounted() {
    //获取sku列表方法
    this.getSkuList();
  },
  methods: {
    //获取sku列表数据
    async getSkuList(page = 1) {
      this.page = page;
      let result = await this.$API.sku.reqSkuList(this.page, this.limit);
      if (result.code == 200) {
        this.records = result.data.records;
        this.total = result.data.total;
      }
    },
    handleSizeChange(limit) {
      this.limit = limit;
      this.getSkuList();
    },
    async cancel(row) {
      let result = await this.$API.sku.reqCancelSale(row.id);
      if (result.code == 200) {
        row.isSale = 0;
        this.$message({
          type: "success",
          message: "下架成功",
        });
      }
    },
    async sale(row) {
      let result = await this.$API.sku.reqSale(row.id);
      if (result.code == 200) {
        row.isSale = 1;
        this.$message({
          type: "success",
          message: "上架成功",
        });
      }
    },
    edit() {
      this.$message("正在开发中……");
    },
    //获取sku详情的方法
    async getSkuIfo(sku) {
      //展示抽屉
      this.show = true;
      //获取数据
      let result = await this.$API.sku.reqSkuById(sku.id);
      if (result.code == 200) {
        this.skuInfo = result.data;
      }
    },
  },
};
</script>

<style scoped>
.buttons {
  margin-right: 10px;
}
.el-row .el-col-5 {
  font-size: 18px;
  text-align: right;
  font-weight: 700;
}
.el-row .el-col {
  margin: 10px 10px;
}
.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}
.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}
>>> .el-carousel__button {
  width: 10px;
  height: 10px;
  background-color: #66ccff;
  border-radius: 50%;
}
.carousel-img {
  width: 100%;
  object-fit: cover;
}
</style>
